<?php

namespace app\index\controller;

use app\commons\model\CreateVideo as CreateVideoModel;
use app\job\UploadFileVideo;
use think\Exception;
use think\Db;
use think\exception\ValidateException;
use think\Queue;

class Createvideo extends Main
{
    /**
     * 生成视频
     */
    public function create()
    {
        $validate = new \app\index\validate\Createvideo();
        if (!$validate->check($this->data)) {
            throw new ValidateException($validate->getError());
        }
        $MaterialModel = new CreateVideoModel();
        $sale = \app\commons\model\Sale::where(['id' => $this->data['sale_id'], 'member_id' => $this->merch_id])->find();
        if (!$sale) return jsonp_error(422, '非法操作！');
        if ($MaterialModel->where(['sale_id' => $this->data['sale_id'], 'is_complete' => 0])->value('id')) {
            return jsonp_error(422, '还有未处理完成的视频，请等待处理完成！');
        }
        if (!empty($this->data)) {
            Db::startTrans();
            try {

                $this->data['yun_type'] = 0;
                $this->data['video'] = json_encode($this->data['video']);
                $res = $MaterialModel->allowField(true)->save($this->data);
                if (!$res) return jsonp_error(422, $MaterialModel->getError());
                //加入到视频素材表
                $create_video_id = $MaterialModel->id;
                $dataAll = [];
                $video = json_decode($this->data['video'], true);
                if (!empty($video)) {
                    if (isset($video['head']) && !empty($video['head'])) {
                        foreach ($video['head'] as $head) {
                            $dataAll[] = [
                                'path' => $head['path'],
                                'sale_id' => $this->data['sale_id'],
                                'yun_type' => $this->data['yun_type'],
                                'position' => 0,
                                'create_video_id' => $create_video_id,
                                'title' => $this->data['title'],
                                'user_id' => $this->merch_id,
                                'type' => $this->merch_id,
                            ];
                        }
                    }

                    if (isset($video['tail']) && !empty($video['tail'])) {
                        foreach ($video['tail'] as $head) {
                            $dataAll[] = [
                                'path' => $head['path'],
                                'sale_id' => $this->data['sale_id'],
                                'yun_type' => $this->data['yun_type'],
                                'position' => 2,
                                'create_video_id' => $create_video_id,
                                'title' => $this->data['title'],
                                'user_id' => $this->merch_id,
                                'type' => $this->merch_id,
                            ];
                        }
                    }

                    if (isset($video['list']) && !empty($video['list'])) {
                        foreach ($video['list'] as $head) {
                            $dataAll[] = [
                                'path' => $head['path'],
                                'sale_id' => $this->data['sale_id'],
                                'yun_type' => $this->data['yun_type'],
                                'position' => 1,
                                'create_video_id' => $create_video_id,
                                'title' => $this->data['title'],
                                'user_id' => $this->merch_id,
                                'type' => $this->merch_id,
                            ];
                        }
                    }
                }
                if (!empty($dataAll)) {
                    $m = new \app\commons\model\Material();
                    $mRes = $m->saveAll($dataAll);
                    if (!$mRes) throw new Exception('生成视频失败！');
                }
                Db::commit();
                //入队列生成合成视频
                Queue::push(UploadFileVideo::class,['id'=>$create_video_id]);
                return jsonp([]);
            } catch (Exception $exception) {
                Db::rollback();
                return jsonp_error(422, $exception->getMessage());
            }
        }
        return jsonp_error(422, '添加失败！');
    }
}